﻿
<Serializable()> Public Class iWatchSource
    Public FileQueue As New List(Of String)
    Public SourceName As String
    Public ExtensionFilter

    Private watcher As New IO.FileSystemWatcher()

    Public WatchEnabled As Integer
    Public WatchPath As String

    Public DestinationEnabled As Integer
    Public DestinationPath As String

    Public FFMPEGEnabled As Integer
    Public FFMPEGOptions As String
    Public FFMPEGTempFolder As String
    Public FFMPEGNewExtension As String

    Public iTunesEnabled As Integer
    Public iTunesTypeCast As Integer
    Public iTunesDeleteAfterCopy As Integer
    Public iTunesType As String

    Public DeleteFlag As Boolean = False



    Public ReadOnly Property WatchSourceName()
        Get
            Return SourceName
        End Get
    End Property

    Public ReadOnly Property MarkedForDeletion()
        Get
            Return DeleteFlag
        End Get
    End Property

    Public Sub MarkForDeletion()
        DeleteFlag = True
    End Sub

    Public Function EditProperties() As Boolean
        Dim WSD = New iWatchSettings
        WSD.SourceName.Text = SourceName
        WSD.ExtensionFilter.Text = ExtensionFilter
        WSD.WatchEnabled.CheckState = WatchEnabled
        WSD.WatchPath.Text = WatchPath
        WSD.DestinationEnabled.CheckState = DestinationEnabled
        WSD.DestinationPath.Text = DestinationPath
        WSD.FFMPEGEnabled.CheckState = FFMPEGEnabled
        WSD.FFMPEGOptions.Text = FFMPEGOptions
        WSD.FFMPEGTempFolder.Text = FFMPEGTempFolder
        WSD.FFMPEGNewExtension.Text = FFMPEGNewExtension
        WSD.iTunesEnabled.CheckState = iTunesEnabled
        WSD.iTunesTypeCast.CheckState = iTunesTypeCast
        WSD.iTunesDeleteAfterCopy.CheckState = iTunesDeleteAfterCopy
        WSD.iTunesType.SelectedText = iTunesType



        If WSD.ShowDialog = DialogResult.OK Then

            SourceName = WSD.SourceName.Text
            ExtensionFilter = WSD.ExtensionFilter.Text
            WatchEnabled = WSD.WatchEnabled.CheckState
            WatchPath = WSD.WatchPath.Text
            DestinationEnabled = WSD.DestinationEnabled.CheckState
            DestinationPath = WSD.DestinationPath.Text
            FFMPEGEnabled = WSD.FFMPEGEnabled.CheckState
            FFMPEGOptions = WSD.FFMPEGOptions.Text
            FFMPEGTempFolder = WSD.FFMPEGTempFolder.Text
            FFMPEGNewExtension = WSD.FFMPEGNewExtension.Text
            iTunesEnabled = WSD.iTunesEnabled.CheckState
            iTunesTypeCast = WSD.iTunesTypeCast.CheckState
            iTunesDeleteAfterCopy = WSD.iTunesDeleteAfterCopy.CheckState
            iTunesType = WSD.iTunesType.SelectedText

            RestartWatcher()
            Return True
        Else
            Return False
        End If
    End Function

    Protected Sub New()

    End Sub

    Public Sub New(ByVal DisableDialog As Boolean)
        If DisableDialog = False Then
            Dim WSD = New iWatchSettings
            If WSD.ShowDialog = DialogResult.OK Then
                SourceName = WSD.SourceName.Text
                ExtensionFilter = WSD.ExtensionFilter.Text
                WatchEnabled = WSD.WatchEnabled.CheckState
                WatchPath = WSD.WatchPath.Text
                DestinationEnabled = WSD.DestinationEnabled.CheckState
                DestinationPath = WSD.DestinationPath.Text
                FFMPEGEnabled = WSD.FFMPEGEnabled.CheckState
                FFMPEGOptions = WSD.FFMPEGOptions.Text
                FFMPEGTempFolder = WSD.FFMPEGTempFolder.Text
                FFMPEGNewExtension = WSD.FFMPEGNewExtension.Text
                iTunesEnabled = WSD.iTunesEnabled.CheckState
                iTunesTypeCast = WSD.iTunesTypeCast.CheckState
                iTunesDeleteAfterCopy = WSD.iTunesDeleteAfterCopy.CheckState
                iTunesType = WSD.iTunesType.SelectedText
                RestartWatcher()
            Else
                Throw New ApplicationException("User Canceled.  Not initialized.")
            End If
        End If



    End Sub


    Sub RestartWatcher()
        watcher.Dispose()
        watcher = New IO.FileSystemWatcher()
        watcher.Filter = ExtensionFilter
        watcher.Path = WatchPath
        watcher.IncludeSubdirectories = True
        watcher.NotifyFilter = (IO.NotifyFilters.LastWrite Or IO.NotifyFilters.FileName Or IO.NotifyFilters.DirectoryName)

        AddHandler watcher.Changed, AddressOf QueueFile
        AddHandler watcher.Created, AddressOf QueueFile
        AddHandler watcher.Renamed, AddressOf QueueFile
        If WatchEnabled = 1 Then
            watcher.EnableRaisingEvents = True
        End If


    End Sub



    Sub QueueFile(ByVal sender As Object, ByVal e As IO.FileSystemEventArgs)
        If Not FileQueue.Contains(e.FullPath) Then
            FileQueue.Add(e.FullPath)
        End If
    End Sub



End Class
